//
//  DataBaseTool.m
//  Baymax
//
//  Created by jolin on 16/4/21.
//  Copyright © 2016年 jolin. All rights reserved.
//

#import "DataBaseTool.h"
#import "UserInfo.h"

FMDatabase *__database = nil;
@implementation DataBaseTool

+ (void)initDataBase {
    __database = [[FMDatabase alloc]initWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/baymax.sqlite"]];
    [__database open];
    [__database executeUpdate:@"CREATE TABLE IF NOT EXISTS user(user_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, user_phone TEXT NOT NULL, user_psw TEXT NOT NULL, user_nickname TEXT, user_photo TEXT, user_sex TEXT, user_name TEXT, user_token TEXT)"];
    [__database close];
}

+ (void)insertUserInfo:(UserInfo *)user {
    [__database open];
    [__database executeUpdate:@"INSERT INTO user(user_phone,user_psw,user_nickname,user_photo,user_sex,user_name,user_token) VALUES (?,?,?,?,?,?,?)",user.phone,user.password,user.nickname,user.photo,user.sex,user.username,user.token];
    [__database close];
}

+ (UserInfo *)getUserInfo {
    [__database open];
    FMResultSet *set = [__database executeQuery:@"SELECT * FROM user"];
    [set next];
    NSString *phone = [set stringForColumn:@"user_phone"];
    NSString *psw = [set stringForColumn:@"user_psw"];
    NSDictionary *dic = @{@"phone":phone, @"psw":psw};
    UserInfo *userInfo = [UserInfo userInfoWithDic:dic];
    return userInfo;
    
}


@end
